﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using System.Xml;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using System.IO;
using DevComponents.DotNetBar;
using PlanningGIS.Framework;



using ESRI.ArcGIS.SystemUI;
using PlanningGIS.Win.Controls;
using PlanningGIS.Util.Convertor;
using PlanningGIS.Engine.Controls;
using PlanningGIS.ArcGIS;
using PlanningGIS.Framework.Rule;
using PlanningGIS.Util.Log;
using PlanningGIS.Framework.Config;

namespace PlanningGIS.DataCheck.form
{
   
    public partial class FrmDataCheckConfig : Office2007Form
    {
        private IMapControl3 pMapControl;
        private IMap pMap;
        private XmlDocument xmlDoc;
        private string PATH;
        IApplication m_Application;

        public FrmDataCheckConfig(IApplication value)
        {
            InitializeComponent();
            txtScale.KeyPress += ControlVerify.txtDoubleBox_KeyPress;
            m_Application = value;
            if ((m_Application as IFramework).IsMapCtrlactive)
            {
                btnGetCurrentScale.Enabled = true;
                pMapControl = (m_Application as IFramework).MapControl;
                pMap = pMapControl.Map;
            }
            else btnGetCurrentScale.Enabled = false;

            PATH = AppConfig.ConfigPath + "\\DataCheckConfig.xml";
        }

        public FrmDataCheckConfig(IMap value)
        {
            InitializeComponent();
            txtScale.KeyPress += ControlVerify.txtDoubleBox_KeyPress;

            pMap = value;

            btnGetCurrentScale.Enabled = (pMap != null);

        }
        

        /// <summary>
        /// 初始化常规配置，包括检查方案、入库方案、分类代码名称、缩放比例尺、记录错误方式
        /// </summary>
        private void InitGeneralConfig()
        {
            try
            {
                cbxDataImport.Items.Clear();
                List<DataImportRule> dataImportRules = RuleConfig.DataBuilding.BuildingRules;
                if (dataImportRules == null || dataImportRules.Count == 0)
                {
                    MsgHelper.ShowInfomationMsgbox("规则文件中没有任何入库规则！");
                    return;
                }
                foreach (DataImportRule dataImportRule in dataImportRules)
                {
                    cbxDataImport.Items.Add(dataImportRule.RuleName);
                }

                comboBoxExWorkSpace.Items.Clear();
                List<DataCheckRule> dataCheckRlues = DataCheckHelper.DataCheck.DataCheckRules;
                if (dataCheckRlues == null || dataCheckRlues.Count == 0)
                {
                    MsgHelper.ShowInfomationMsgbox("规则文件中没有任何监理规则！");
                    return;
                }

                foreach (DataCheckRule dataCheckRule in dataCheckRlues)
                {
                    comboBoxExWorkSpace.Items.Add(dataCheckRule);
                }

                if (comboBoxExWorkSpace.Items.Count > 0)
                {
                    for (int i = 0; i < comboBoxExWorkSpace.Items.Count; i++)
                    {
                        DataCheckRule dataCheckRule = comboBoxExWorkSpace.Items[i] as DataCheckRule;
                        if (dataCheckRule != null)
                            if (dataCheckRule.IsDefault)
                            {
                                comboBoxExWorkSpace.SelectedIndex = i;
                                return;
                            }
                    }
                    comboBoxExWorkSpace.SelectedIndex = 0;
                }
            }
            catch (Exception err)
            {
                MsgHelper.ShowWarningMsgbox(err.Message);
            }
        }

        private void btnGetCurrentScale_Click(object sender, EventArgs e)
        {
            try
            {
                txtScale.Text = pMap.MapScale.ToString("##.###");
            }
            catch { }
        }

        private void comboBoxExWorkSpace_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataCheckRule dataCheckRule = comboBoxExWorkSpace.SelectedItem as DataCheckRule;
            if (dataCheckRule == null) return;

            cbxDataImport.Text = dataCheckRule.DataImportRuleName;
            txtFCODE.Text = dataCheckRule.FcodeName;
            txtScale.Text = dataCheckRule.Scale.ToString();

            rdoPoint.Checked = (dataCheckRule.MarkType == "Point" ? true : false);
            rdoShape.Checked = (dataCheckRule.MarkType == "Shape" ? true : false);

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            #region 常规设置
            if (txtFCODE.Text == "")
            {
                MsgHelper.ShowInfomationMsgbox("请输入分类代码属性字段的名称");
                return;
            }
            if (comboBoxExWorkSpace.Text == "")
            {
                MsgHelper.ShowInfomationMsgbox("未设置检查方案！");
                return;
            }

            if (cbxDataImport.Text == "")
            {
                MsgHelper.ShowInfomationMsgbox("未设置入库方案！");
                return;
            }

            if (txtScale.Text == "")
            {
                MsgHelper.ShowInfomationMsgbox("请设置缩放比例尺！");
                return;
            }

            List<DataCheckRule> dataCheckRlues = DataCheckHelper.DataCheck.DataCheckRules;
            if (dataCheckRlues == null || dataCheckRlues.Count == 0)
            {
                MsgHelper.ShowInfomationMsgbox("规则文件中没有任何监理规则！");
                return;
            }

            foreach (DataCheckRule item in dataCheckRlues)
            {
                item.IsDefault = false;
            }

            DataCheckRule dataCheckRule = comboBoxExWorkSpace.SelectedItem as DataCheckRule;
            if (dataCheckRule == null) return;

            dataCheckRule.IsDefault = true;
            dataCheckRule.DataImportRuleName = cbxDataImport.Text;
            dataCheckRule.FcodeName = txtFCODE.Text;
            dataCheckRule.Scale = Convert.ToDouble(txtScale.Text);
            dataCheckRule.MarkType = (rdoPoint.Checked ? "Point" : "Shape");

            ConfigHelper.SaveObject<DataCheck>(DataCheckHelper.DataCheck, "Rule");
            #endregion

           

            ////为了改变参数后，如果当前工具又是某一个检查项，那么点击“确定”后，需要将修改的参数反映到接下来的检查中，只好用这种变态的方式
            ////来进行强行的刷新操作，确实挺汗的
            //ITool pTool = (m_Application as IFramework).Tool;
            //(m_Application as IFramework).Tool = null;
            //(m_Application as IFramework).Tool = pTool;

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmNewRule frmNewRule = new FrmNewRule();
            if (frmNewRule.ShowDialog() == System.Windows.Forms.DialogResult.OK) InitGeneralConfig();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            DataCheckRule dataCheckRule = comboBoxExWorkSpace.SelectedItem as DataCheckRule;
            if (dataCheckRule == null) return;
            DataCheckHelper.DataCheck.DataCheckRules.Remove(dataCheckRule);
            comboBoxExWorkSpace.Items.Remove(dataCheckRule);

            InitGeneralConfig();
        }

        private void frmDataCheckConfig_Load(object sender, EventArgs e)
        {

            InitGeneralConfig();
            
            try
            {
                if (File.Exists(PATH))
                {
                    xmlDoc = new XmlDocument();
                    xmlDoc.Load(PATH);
                }
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox("检查配置文件有误，可能会导致某些检查功能无法使用！");
                xmlDoc = null;
            }
            
        }

    }
}
